package com.order.www;

/**
 * Creared with IntelliJ IDEA.
 * Description:判断给定的链表中是否有环。如果有环则返回true，否则返回false。
 * User:yxd
 * Date:2022-01-10
 * Time:18:18
 */
//是否有环
public class NC4 {
    public boolean hasCycle(ListNode head) {
        if(head == null)return false;
        ListNode fast = head;
        ListNode slow = head;
        //快慢指针
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;

            if(fast == slow){
                return true;
            }

        }
        return false;
    }
}
